﻿using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace BaoVeBienCuong.SelectMap
{
    class FileThanh
    {
        static string temp;
        static IsolatedStorageFile storage;
        public static void getData(ref Thanh[] thanh)
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_SelectMap.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_SelectMap.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("0;1;0");
                    writer.WriteLine("1;0;0");
                    writer.WriteLine("2;0;0");
                    writer.WriteLine("3;0;0");
                    writer.WriteLine("4;0;0");
                    writer.WriteLine("5;0;0");
                    writer.WriteLine("6;0;0");
                    writer.WriteLine("7;0;0");
                    writer.WriteLine("8;0;0");
                    writer.WriteLine("9;0;0");
                    writer.WriteLine("10;0;0");
                    writer.WriteLine("11;0;0");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_SelectMap.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                do
                {
                    temp = r.ReadLine();
                    string[] result = temp.Split(';');
                    bool t = false;
                    if (result[1].Equals("0")) t = false;
                    else t = true;
                    thanh[Int16.Parse(result[0])].setValue(t, Int16.Parse(result[2]));
                } while (!r.EndOfStream);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
        }
        public static void SaveFile(ref Thanh[] thanh, int IdThanh, int Star)
        {
            thanh[IdThanh].setValue(true, Star);
            if (IdThanh != 11 && !thanh[IdThanh + 1].isOpen())
            {
                thanh[IdThanh + 1].setValue(true, 0);
            }
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("sunnet_l12sq_SelectMap.txt"))
            {
                storage.DeleteFile("sunnet_l12sq_SelectMap.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_SelectMap.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                for (int t = 0; t < 12; t++)
                {
                    writer.WriteLine(t + ";" + (thanh[t].isOpen() == true ? "1" : "0") + ";" + (thanh[t].getStar().ToString()));
                }
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
        public static int getSao()
        {
            int sao = 0;
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("sunnet_l12sq_Sao.txt"))
            {
                using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_Sao.txt", FileMode.Create, FileAccess.Write, storage)))
                {
                    writer.WriteLine("0");
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
            IsolatedStorageFileStream f = storage.OpenFile("sunnet_l12sq_Sao.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            using (StreamReader r = new StreamReader(f))
            {
                temp = r.ReadLine();
                sao = Int16.Parse(temp);
                r.Close();
                r.Dispose();
            }//mở file ra để đọc
            storage.Dispose();
            return sao;
        }
        public static void saveSao(int sao)
        {
            storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (storage.FileExists("sunnet_l12sq_Sao.txt"))
            {
                storage.DeleteFile("sunnet_l12sq_Sao.txt");
            }
            using (var writer = new StreamWriter(
                new IsolatedStorageFileStream("sunnet_l12sq_Sao.txt", FileMode.Create, FileAccess.Write, storage)))
            {
                writer.WriteLine(sao.ToString());
                writer.Flush();
                writer.Close();
                writer.Dispose();
            }
            storage.Dispose();
        }
    }
}
